#encoding:utf-8
require "erb"
require "yaml"
require File.dirname(__FILE__)+"/bean_info"
#require File.dirname(__FILE__)+"/db_mysql"
require "fileutils"
#yaml
YAML_URL = File.dirname(__FILE__)+"/yaml/template.yml"

#java
JAVA_ENTITY = File.dirname(__FILE__)+"/erb/java/entity.erb"
JAVA_DAO = File.dirname(__FILE__)+"/erb/java/dao.erb"
JAVA_SERVICE = File.dirname(__FILE__)+"/erb/java/service.erb"
JAVA_SERVICE_IMPL = File.dirname(__FILE__)+"/erb/java/serviceImpl.erb"
JAVA_ACTION = File.dirname(__FILE__)+"/erb/java/action.erb"
JAVA_DAO_TEST = File.dirname(__FILE__)+"/erb/java/dao_test.erb"
JAVA_SERVICE_TEST = File.dirname(__FILE__)+"/erb/java/service_test.erb"

#jsp
JSP_MAIN = File.dirname(__FILE__)+"/erb/html/index.erb"
JSP_CREATE = File.dirname(__FILE__)+"/erb/html/add.erb"
JSP_EDIT = File.dirname(__FILE__)+"/erb/html/edit.erb"
JSP ={"index"=>JSP_MAIN,"add"=>JSP_CREATE,"edit"=>JSP_EDIT}

class Generate



  def initialize(bean)
    @bean = bean
  end


  def generate(type)
    beanInfo = @bean

    javaBaseUrl = File.dirname(__FILE__)+"/../main/java"
    javaTestBaseUrl = File.dirname(__FILE__)+"/../test/java"
    javaPackageUrl = beanInfo.package.gsub(/\./,"/")

    viewBaseUrl = File.dirname(__FILE__)+"/../main/webapp/WEB-INF/views"

    case  type
      when "entity"
        puts "generate entity..."
        message = initERB(JAVA_ENTITY)
        entityFileUrl=javaBaseUrl+"/#{javaPackageUrl}/entity/#{beanInfo.classname}.java"
        if !Dir.exist?(javaBaseUrl+"/#{javaPackageUrl}/entity")
         FileUtils.makedirs javaBaseUrl+"/#{javaPackageUrl}/entity"
        end
        file= File.new(entityFileUrl, "w")
        file.print message.result(binding).gsub(/\\%/, "%")
        puts "generate entity success..."
      when "dao"
        puts "generate dao..."
        message = initERB(JAVA_DAO)
        daoFileUrl=javaBaseUrl+"/#{javaPackageUrl}/dao/#{beanInfo.classname}Dao.java"
        if !Dir.exist?(javaBaseUrl+"/#{javaPackageUrl}/dao")
          FileUtils.makedirs javaBaseUrl+"/#{javaPackageUrl}/dao"
        end
        file= File.new(daoFileUrl, "w")
        file.print message.result(binding).gsub(/\\%/, "%")
        puts "generate dao success..."

        puts "generate dao test..."
        message = initERB(JAVA_DAO_TEST)
        daoTestFileUrl=javaTestBaseUrl+"/#{javaPackageUrl}/dao/#{beanInfo.classname}DaoTest.java"
        if !Dir.exist?(javaTestBaseUrl+"/#{javaPackageUrl}/dao")
          FileUtils.makedirs javaTestBaseUrl+"/#{javaPackageUrl}/dao"
        end
        file= File.new(daoTestFileUrl, "w")
        file.print message.result(binding).gsub(/\\%/, "%")
        puts "generate dao test success..."

      when "service"
        puts "generate service..."
        message = initERB(JAVA_SERVICE)
        serviceFileUrl=javaBaseUrl+"/#{javaPackageUrl}/service/admin/#{beanInfo.classname}Service.java"
        file= File.new(serviceFileUrl, "w")
        file.print message.result(binding).gsub(/\\%/, "%")
        puts "generate service success..."

        puts "generate serviceImpl..."
        message = initERB(JAVA_SERVICE_IMPL)
        serviceFileUrl=javaBaseUrl+"/#{javaPackageUrl}/service/admin/#{beanInfo.classname}ServiceImpl.java"
        file= File.new(serviceFileUrl, "w")
        file.print message.result(binding).gsub(/\\%/, "%")
        puts "generate serviceImpl success..."


      when "action"
        puts "generate action..."
        message = initERB(JAVA_ACTION)
        actionFileUrl=javaBaseUrl+"/#{javaPackageUrl}/web/#{beanInfo.goalaction}/#{beanInfo.classname}Action.java"
        if !Dir.exist?(javaBaseUrl+"/#{javaPackageUrl}/web/#{beanInfo.goalaction}")
         FileUtils.makedirs javaBaseUrl+"/#{javaPackageUrl}/web/#{beanInfo.goalaction}"
        end
        file= File.new(actionFileUrl, "w")
        file.print message.result(binding).gsub(/\\%/, "%")
        puts "generate action success..."
      when "view"
        puts "generate view..."
        JSP.each do |k,url|
        message = initERB(url)
        viewFileUrl=viewBaseUrl+"/#{beanInfo.goalaction}/#{beanInfo.alias}"
        if !Dir.exist?(viewFileUrl)
         FileUtils.makedirs viewFileUrl
        end
        file= File.new(viewFileUrl+"/#{k}.jsp", "w")
        file.print message.result(binding)
        end
        puts "generate view success..."
      else
        puts "non type!"
    end

  end


  def initERB(template_url)
    template=IO.read(template_url)
    message=ERB.new(template)
    message
  end

end

bean = YAML.load(File.open(YAML_URL))[0]
bean.date=Time.now
generate = Generate.new(bean)

#generate.generate("entity")
#generate.generate("dao")
generate.generate("service")
generate.generate("action")
generate.generate("view")


#db = DbMysql.new bean
#db.create

#beanInfo=YAML.load(File.open(YAML_URL))[0]
#beanInfo.date = Time.now
#puts beanInfo.goalaction
#puts  beanInfo.tablename
#puts beanInfo.properties
#puts beanInfo.date

#path = File.dirname(__FILE__)+"/../main/java/com/badyqoo/dao/NewsDao.java"
#if !File.exist?(path)
#  file= File.new(path, "w")
#  file.print generate.generateJava(JAVA_DAO)
#  #Dir.mkdir(path)
#
#end



